function [TotalPayoff1 Buy1 Sell1 TotalPayoff2 Buy2 Sell2 beta VaRup VaRdown failure_rate] = trading(pStock, pIndex, rPortfolio, rIndex, option, weights, alpha, V0)
    % Calculte return of portfolio
    rP = rPortfolio;
    pP = pStock;
    
    %beta = getBeta_upon_Option(pP, pIndex, rP, rIndex, option);
        
    %VaRup = VaRCov(rPortfolio, rIndex, beta, weights, alpha, V0);
    %VaRdown = VaRCov(rPortfolio, rIndex, beta, weights, 1-alpha, V0);
    
    %Back test VaR
    %failure_rate = BacktestVaR(rP, VaRdown);
    
    
    [lenStock numStock] = size(rP);
    test = 0.75;
    
    % Trong truong hop beta la 1 vecto, tinh VaR cho toan bo thi truong
    %sau do lay 25% du lieu cuoi de so sanh voi return.
    %Note that compapre VaR of day t-1 with return of day t.
    
    % Neu beta la hang so, dung 75% du lieu qua khu de test
     if strcmp(option,'Smoothing') || strcmp(option,'Kalman Filter')
        rPpast = rP;
        rIndexpast = rIndex;
        pPpast = pP;
        pIndexpast = pIndex;
        rPortfoliofullpast = rPortfolio;
    else
        rPpast = rP(1:floor(end * test),:);
        rIndexpast = rIndex(1:floor(end * test));
        pPpast = pP(1:floor(end * test),:);
        pIndexpast = pIndex(1:floor(end * test));
        
        rPortfoliofullpast = rPortfolio(1:floor(end * test),:);
     end
    for k = 1:numStock
        beta_test(:,k) = getBeta_upon_Option(pPpast(:,k), pIndexpast, rPpast(:,k), rIndexpast, option);
    end
        beta = beta_test;
        beta = beta*weights;
%     if strcmp(option,'Smoothing') == 0 && strcmp(option,'Kalman Filter') ==0
%        beta 
%     end
    
    VaRup = VaRCov(rPortfoliofullpast, rIndexpast, beta_test, weights, alpha, V0);
    VaRdown = VaRCov(rPortfoliofullpast, rIndexpast, beta_test, weights, 1-alpha, V0);

    %Lay 25% du lieu return moi nhat cua data de test 
    rPortfolioTest = rP(floor(end*test):end,:);
        
    % Xet 2 TH: VaR la hang so va VaR la mot vecto
    if length(VaRdown) == 1 || length(VaRup) == 1
        %myVaRdown
        %myVaRup
        temp = zeros(length(rPortfolioTest),1);
        temp(:) = VaRdown;
        testVaRdown = temp;
        temp(:) = VaRup;
        testVaRup = temp;
    else
        %Adjust VaR for compapring VaR of day t-1 with return of day t
        testVaRdown = VaRdown(end-length(rPortfolioTest):end-1);
        testVaRup = VaRup(end-length(rPortfolioTest):end-1);
    end          
    
    
    
    %Back test VaR
    failure_rate = BacktestVaR(rP*weights, VaRdown);
    rPortfolioTest = rPortfolioTest*weights;
    
    % Calculate total payoff upon strategy
    % Plot VaR and return
    figure('name', 'Strategy Buy-Sell');
    hold on
    plot(testVaRup,'--r','LineWidth',2);
    plot(testVaRdown,'--g','LineWidth',2);
    plot(rPortfolioTest,'-b','LineWidth',1);
    xlabel('Days');
    ylabel('Return');

    title(strcat('Strategy 1-',option),'FontWeight','bold');
    
    [TotalPayoff1 Buy1 Sell1] = tradingStrategy1(rPortfolioTest, testVaRup, V0);
    
    Buy1ve = find(Buy1~=0),Buy1(Buy1~=0);
    Sell1ve = find(Sell1~=0),Sell1(Sell1~=0);
    plot(Sell1ve,rPortfolioTest(Sell1ve),'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',7);
    plot(Buy1ve,rPortfolioTest(Buy1ve),'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',7);
    legend('VaR 95%','VaR 5%','Return(%)','Buy','Sell','Location','SouthWest');
    
    
%     grid on        
%     set(gca,'Xtick',0:4:length(rPortfolioTest));
        
    figure('name', 'Strategy Buy-Sell');
    hold on
    plot(testVaRup,'--r','LineWidth',2);
    plot(testVaRdown,'--g','LineWidth',2);
    plot(rPortfolioTest,'-b','LineWidth',1);
    xlabel('Days');
    ylabel('Return');
    
    title(strcat('Strategy 2-',option),'FontWeight','bold');
    
    [TotalPayoff2 Buy2 Sell2] = tradingStrategy2(rPortfolioTest, testVaRup, testVaRdown, V0);
    
    Buy2ve = find(Buy2~=0),Buy2(Buy2~=0);
    Sell2ve = find(Sell2~=0),Sell2(Sell2~=0);
    plot(Sell2ve,rPortfolioTest(Sell2ve),'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',7);
    plot(Buy2ve,rPortfolioTest(Buy2ve),'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',7);
    legend('VaR 95%','VaR 5%','Return(%)','Buy','Sell','Location','SouthWest');
    
    